<?php namespace Xcx;

// 小程序公共类 - 管理篇
class Member extends \Xcx\Common
{

    public $xcx_field;

    public function __construct(...$params)
    {
        parent::__construct(...$params);
        if ($this->uid != $this->xcx['uid']) {
            $this->_msg(0, '你没有权限操作此小程序');
        }

        $uri = \Phpcmf\Service::L('router')->class . '/'. \Phpcmf\Service::L('router')->method;
        $menu = require APPPATH.'Config/Menu.php';
        $uris = $mymenu = [];
        $meta_title = '';
        foreach ($menu as $i => $top) {
            $menu[$i]['id'] = 't'.$i;
            foreach ($top['link'] as $j => $link) {
                $menu[$i]['link'][$j]['id'] = 'l'.$i.'-'.$j;
                $menu[$i]['link'][$j]['pid'] = $i;
                $uris[$link['uri']] = $menu[$i]['link'][$j];
                if ($uri == $link['uri']) {
                    $mymenu[] = $menu[$i]['link'][$j]['id'];
                    $mymenu[] = $menu[$i]['id'];
                    $meta_title = $link['name'].'-'.$top['name'].'-'.$this->xcx['name'];
                }
            }
        }

        if (!$meta_title && isset($uris[\Phpcmf\Service::L('router')->class . '/index']) && $uris[\Phpcmf\Service::L('router')->class . '/index']) {
            // 默认index方法
            $link = $uris[\Phpcmf\Service::L('router')->class . '/index'];
            $mymenu[] = $link['id'];
            $mymenu[] = $menu[$link['pid']]['id'];
            $meta_title = $link['name'].'-'.$menu[$link['pid']]['name'].'-'.$this->xcx['name'];
        }

        if (!$meta_title) {
            $meta_title = $this->xcx['name'];
        }

        $this->xcx_field = [
            'logo' => [
                'name' => '小程序图标',
                'ismain' => 1,
                'ismember' => 1,
                'fieldtype' => 'File',
                'fieldname' => 'logo',
                'setting' => [
                    'option' => ['ext' => 'jpg,gif,png,jpeg', 'size' => 1, 'input' => 1],
                    'validate' =>
                        array (
                            'required' => '1',
                            'tips' => '上传80x80矩形App图标',
                        ),
                ]
            ],
            'version' => array (
                'fieldname' => 'version',
                'name' => '版本号',
                'ismain' => '1',
                'ismember' => 1,
                'fieldtype' => 'Text',
                'setting' =>
                    array (
                        'option' =>
                            array (
                                'fieldtype' => '',
                                'fieldlength' => '',
                                'value' => '',
                                'width' => '',
                                'css' => '',
                            ),
                        'validate' =>
                            array (
                                'required' => '1',
                                'pattern' => '',
                                'errortips' => '',
                                'check' => '',
                                'filter' => '',
                                'formattr' => '',
                                'tips' => '版本号，例如：4.2.0',
                            ),
                    ),
            ),
        ];

        \Phpcmf\Service::V()->assign([
            'uri' => $uri,
            'menu' => $menu,
            'mymenu' => $mymenu,
            'prefix' => \Phpcmf\Service::L('router')->class,
            'meta_title' => $meta_title,
        ]);
    }

    // 基本信息存储
    protected function _config_save() {

        $field = $this->xcx_field;
        // 初始化自定义字段类
        \Phpcmf\Service::L('field')->app(APP_DIR);

        if (IS_POST) {
            $post = \Phpcmf\Service::L('input')->post('data', true);
            $name = \Phpcmf\Service::L('input')->post('name', true);
            if (!$name) {
                $this->_json(0, dr_lang('名称没有填写'), ['field' => 'name']);
            } elseif (strlen($name) > 40) {
                $this->_json(0, dr_lang('名称太长了'), ['field' => 'name']);
            }
            list($data, $return, $attach) = \Phpcmf\Service::L('form')->id($this->xcx['id'])->validation($post, null, $field, $this->xcx['setting']['config']);
            // 输出错误
            $return && $this->_json(0, $return['error'], ['field' => $return['name']]);
            // 附件归档
            if (SYS_ATTACHMENT_DB) {
                list($post, $return, $attach) = \Phpcmf\Service::L('form')->validation($post, null, $field);
                $attach && \Phpcmf\Service::M('Attachment')->handle($this->xcx['id'], \Phpcmf\Service::M()->dbprefix('xcx'), $attach);
            }
            $this->xcx['setting']['config'] = $data[1];
            \Phpcmf\Service::M()->table('xcx')->update($this->xcx['id'], [
                'name' => dr_safe_replace($name),
                'logo' => $data[1]['logo'],
                'setting' => dr_array2string($this->xcx['setting']),
            ]);
            $this->_json(1, dr_lang('保存成功'));
        }

        \Phpcmf\Service::V()->assign([
            'myfield' => \Phpcmf\Service::L('field')->toform($this->xcx['id'], $field, $this->xcx['setting']['config']),
        ]);

    }

    // 接口信息存储
    protected function _config_api_save() {

        if (IS_POST) {
            $this->xcx['setting']['api'] = \Phpcmf\Service::L('input')->post('data', true);
            \Phpcmf\Service::M()->table('xcx')->update($this->xcx['id'], [
                'setting' => dr_array2string($this->xcx['setting']),
            ]);
            $this->_json(1, dr_lang('保存成功'));
        }

        \Phpcmf\Service::V()->assign([
            'data' => $this->xcx['setting']['api'],
            'api_file' => dr_get_app_dir('xcx').'Extends/config_api.html',
        ]);
    }


}

